<?
include('/var/www/html/new/d/common/header.html');
include('/var/www/html/new/d/common/func.inc.php');
include('/var/www/html/new/d/common/alliance_fid.php');
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/class/ProOrder.php';
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array('location' => 'http://localhost/open/openService/pft_insideMX.php','uri' => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$aom = 'aid';
// $alli_names = $fids;
//用于选择框里的联盟名字
if($_SESSION['sid']!=1)
$sel = "SELECT dtitle,fid from pft_distributor_union_SE where fid in($fid) ";

else
    $sel = "SELECT dtitle,fid from pft_distributor_union_SE";
$GLOBALS['le']->query($sel);
while($row= $GLOBALS['le']->fetch_assoc()){
    $alli_names[$row['fid']] = $row['dtitle'];
}
            
$titleT=array("lid"=>"景区名称","tid"=>"票类名称","mid"=>"分销商名称");
//查询联盟名称


echo "<pre>";
//var_export($id_case);
//print_r($fids);
echo "</pre>";
// exit;
$where = ' where 1=1 ';
if($id_case){  
//管理员进来的时候是不会有这个变量的 
//其他人如果没有这个变量的话在前面就exit了
//所以有这个变量的能到这的都是普通用户 
    if($_GET['alli_num']){//选择了联盟号码时
        $alli_num = safehtml($_GET['alli_num']);
        foreach($id_case as $k => $v){//$k是lirun表里身份的字段 
            $id_case_2[$k] = explode(',',$v);//当所选的联盟号码在这个人的身边字段里的数组里时 往下继续执行
            if(in_array($alli_num,$id_case_2[$k])){
                switch($k){
                    case 'member' : //是member(只是这个联盟里普通的成员 不是任何管理层)
                        $where .= " and fid={$alli_num} and applyid={$_SESSION['sid']} ";
                        break 2;
                    case 'fid' :  //是fid(盟主)
                        $where .= " and fid={$alli_num} ";
                        break 2;
                    default :
                        $where .= " and fid={$alli_num} and $k={$_SESSION['sid']} ";
                        break 2;                
                }
            }
        }
    }else{
        $where .= ' and (';
        if($id_case['agencyID_C'])//市代
            $where1 = " (fid in ({$id_case['agencyID_C']}) and agencyID_C={$_SESSION['sid']}) OR";
        if($id_case['agencyID_P'])//省代
            $where2 = " (fid in ({$id_case['agencyID_P']}) and agencyID_P={$_SESSION['sid']}) OR";
        if($id_case['fid']){//盟主
            $where3 = " (fid in ({$id_case['fid']})) OR";
        }
        if($id_case['member'])//供应商
            $where4 = " (fid in ({$id_case['member']}) and applyid={$_SESSION['sid']}) OR";
        $where .=substr($where1.$where2.$where3.$where4,0,-2).')';
    }
}else if($_GET['alli_num']){
    $alli_num = safehtml($_GET['alli_num']);
    $where .= " and fid=$alli_num ";
}
$where_time = '';
if($_GET['begintime'])
    $where_time = " and rectime >'{$_GET['begintime']} 00:00:00' ";
if($_GET['endtime'])
    $where_time .= " and rectime <'{$_GET['endtime']} 23:59:59' ";
   
$code_area = array();
// $sel_lirun = 'select orderid,adminid,applyid,fid,agencyID_P,agencyID_C,inviterID,P_inviterID,adminid_lirun,apply_lirun,fid_lirun,p_agency_lirun,c_agency_lirun,inviter_lirun,p_inviter_lirun from pft_distributor_union_lirun'.$where;
$sel_lirun = 'select orderid,fid from pft_distributor_union_lirun'.$where.$where_time;
//echo $sel_lirun;
$GLOBALS['le']->query($sel_lirun);
unset($row);
$orderid_1 = array();
$id_case_array['member'] = explode(',',$id_case['member']);
//到这先查出除了分销商之外的所有订单  这些订单都是需要统计的
//这之后查出分销商的订单

//按选择的统计方式来判断
switch($_GET['by']){
    case 'province':
    default:
//        echo "<pre>";
        while($row= $GLOBALS['le']->fetch_assoc()){
//            var_dump($row);
            $orderid_1[$row['orderid']] = $row['orderid'];
            $x = simplexml_load_string($s->Order_Globle_Search(
                    '', '', '', '', '', '', '', '', '', '',
                    '', '', $row['orderid'], '', '', '', '', '', '', '',
                    '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
            $x = pmode($x);
            if((int)$x->Rec->UUpmode == 3)
                $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
            $arrId = (int)$x->Rec->UUareacode;
            ++$result_tmp[$row['fid']][$arrId]['order'];
            $result_tmp[$row['fid']][$arrId]['fid'] = $row['fid'];
            $result_tmp[$row['fid']][$arrId]['tnum'] += $x->Rec->UUtnum;
            $result_tmp[$row['fid']][$arrId]['money'] += (int)$x->Rec->UUtnum*(int)$x->Rec->UUtprice;
            switch((int )$x->Rec->UUpmode){
                case 0:// 余额
                    $result_tmp[$row['fid']][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 1:// 支付宝
                    $result_tmp[$row['fid']][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 2:// 授信支付
                    $result_tmp[$row['fid']][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 3: // 自销
                    $result_tmp[$row['fid']][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 4:// 现场支付
                    $result_tmp[$row['fid']][$arrId]['pmode4'] +=(int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                default:
                    break;
            }
        }

//        echo "<pre>";
//        print_r($result_tmp);
        $alli_num = $id_case['member'];//字符串 逗号隔开的形式
        if($_GET['alli_num'])
            $alli_num = in_array($_GET['alli_num'],$id_case_array['member']) ? $_GET['alli_num'] : '';
         if($alli_num){
            $sel_lirun = "select orderid,fid from pft_distributor_union_lirun where fid in({$alli_num})".$where_time;
//             echo $sel_lirun;
            $GLOBALS['le']->query($sel_lirun);
            while($row = $GLOBALS['le']->fetch_assoc()){
                var_dump($row);
                if(!$orderid_1[$row['orderid']]){//这里查出来的订单 都是分销商的
                     $x = simplexml_load_string($s->Order_Globle_Search(
                            '', '', '', '', '', '', '', '', '', '',
                            '', '', $row['orderid'], '', '', '', '', '', '', '',
                            '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
//                    if((int)$x->Rec->UUmid == $_SESSION['sid']){//判断下这个订单的分销商是不是登录者的 不是就unset
                        $x = pmode($x);
                        if((int)$x->Rec->UUpmode == 3)
                            $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
                        $arrId = (int)$x->Rec->UUareacode;
                        ++$result_tmp[$row['fid']][$arrId]['order'];
                        $result_tmp[$row['fid']][$arrId]['tnum'] += $x->Rec->UUtnum;
                        $result_tmp[$row['fid']][$arrId]['money'] += (float)$x->Rec->UUtnum*(float)$x->Rec->UUtprice;
                        switch((int )$x->Rec->UUpmode){
                            case 0:// 余额
                                $result_tmp[$row['fid']][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 1:// 支付宝
                                $result_tmp[$row['fid']][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 2:// 授信支付
                                $result_tmp[$row['fid']][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 3: // 自销
                                $result_tmp[$row['fid']][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 4:// 现场支付
                                $result_tmp[$row['fid']][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            default:
                                break;
//                        }
                    }
                }
            }
        }

//    echo "</pre>";
//        print_r($result_tmp);
//        echo "</pre>";
       // unset($fids);
       // unset($fid);
       if(count($result_tmp)){
       foreach($result_tmp as $key=>$val){
            foreach($val as $k=>$v){
                $code = $k;
                if(!$code_area[$key][$code]){//没查询过这个地区
                    $sel_province_city = "select province,city,parent_province from pft_area_code_concat
                                        where n_code=$code limit 1";
                    $GLOBALS['le']->query($sel_province_city);
                    $GLOBALS['le']->fetch_assoc();
                    $code_area[$key][$code]=array(
                            'code'=>    $code,
                            'province'=>$GLOBALS['le']->f('province'),
                            'city'=>    $GLOBALS['le']->f('city'),
                            'parent_province'=>$GLOBALS['le']->f('parent_province')
                    );

                    $sel_agency_p = "select inviterID from pft_distributor_agency where fid={$key} and 
                                            darea_code={$code_area[$key][$code]['parent_province']} limit 1";
                    $GLOBALS['le']->query($sel_agency_p);
                    $GLOBALS['le']->fetch_assoc();
                    $agency_p = $GLOBALS['le']->f('inviterID');//省代理不存在 则取盟主
                    $agency_p = $agency_p!='' ? $agency_p : $key;
                    $code_area[$key][$code]['agency_p']=$agency_p;
                    $agency[$code_area[$key][$code]['agency_p']] = $agency_p;
                    
                }
                $result[$key][$code_area[$key][$code]['parent_province']]['title']=  $code_area[$key][$code]['province'];
                $result[$key][$code_area[$key][$code]['parent_province']]['agency_p']= $code_area[$key][$code]['agency_p'];
                $result[$key][$code_area[$key][$code]['parent_province']]['order']  += $v['order'];
                $result[$key][$code_area[$key][$code]['parent_province']]['tnum']   += $v['tnum'];
                $result[$key][$code_area[$key][$code]['parent_province']]['money']  += $v['money'];
                $result[$key][$code_area[$key][$code]['parent_province']]['pmode0'] += $v['pmode0'];
                $result[$key][$code_area[$key][$code]['parent_province']]['pmode1'] += $v['pmode1'];
                $result[$key][$code_area[$key][$code]['parent_province']]['pmode2'] += $v['pmode2'];
                $result[$key][$code_area[$key][$code]['parent_province']]['pmode3'] += $v['pmode3'];
                $result[$key][$code_area[$key][$code]['parent_province']]['pmode4'] += $v['pmode4'];
            }
            // $fid .=$key.',';
        }}
        // $fid = substr($fid,0,-1);
        // if($fid){
            // $sel = "SELECT dtitle,fid from pft_distributor_union_SE where fid in($fid) ";
            // $GLOBALS['le']->query($sel);
            // while($row= $GLOBALS['le']->fetch_assoc()){
                // $fids[$row['fid']] = $row['dtitle'];
            // }
        // }

//        if($_GET['cs']){
//            echo "<pre>";
//             print_r($agency);
//            print_r($result);
//            echo "</pre>";
            // exit;
//        }
        break;        

    case 'city':        
        while($row= $GLOBALS['le']->fetch_assoc()){
            $orderid_1[$row['orderid']] = $row['orderid'];
            $x = simplexml_load_string($s->Order_Globle_Search('', '', '', '', '', '', '', '', '', '',
                                                    '', '', $row['orderid'], '', '', '', '', '', '1', '1',
                                                    '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
            $x = pmode($x);
            if((int)$x->Rec->UUpmode == 3)
                $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
            $arrId = (int)$x->Rec->UUareacode;
            ++$result[$row['fid']][$arrId]['order'];
            $result[$row['fid']][$arrId]['tnum'] += $x->Rec->UUtnum;
            $result[$row['fid']][$arrId]['money'] += (float)$x->Rec->UUtnum*(float)$x->Rec->UUtprice;
            switch((int )$x->Rec->UUpmode){
                case 0:// 余额
                    $result[$row['fid']][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 1:// 支付宝
                    $result[$row['fid']][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 2:// 授信支付
                    $result[$row['fid']][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 3: // 自销
                    $result[$row['fid']][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 4:// 现场支付
                    $result[$row['fid']][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                default:
                    break;
            }
        }
//        exit;
        $alli_num = $id_case['member'];
        if($_GET['alli_num'])
            $alli_num = in_array($_GET['alli_num'],$id_case_array['member']) ? $_GET['alli_num'] : '';
//        var_dump($alli_num);
         if($alli_num){
            $sel_lirun = "select orderid,fid from pft_distributor_union_lirun where fid in({$alli_num})".$where_time;
            $GLOBALS['le']->query($sel_lirun);
            while($row = $GLOBALS['le']->fetch_assoc()){
                if(!in_array($row['orderid'],$orderid_1)){//这里查出来的订单 都是分销商的
                     $x = simplexml_load_string($s->Order_Globle_Search('', '', '', '', '', '', '', '', '', '',
                                                                '', '', $row['orderid'], '', '', '', '', '', '', '',
                                                                '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
//                    if((int)$x->Rec->UUmid == $_SESSION['sid']){//判断下这个订单的分销商是不是登录者的 不是就unset
                        $x = pmode($x);
                        if((int)$x->Rec->UUpmode == 3)
                            $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
                        $arrId = (int)$x->Rec->UUareacode;
                        ++$result[$row['fid']][$arrId]['order'];
                        $result[$row['fid']][$arrId]['tnum'] += $x->Rec->UUtnum;
                        $result[$row['fid']][$arrId]['money'] += (int)$x->Rec->UUtnum*(int)$x->Rec->UUtprice;
                        switch((int )$x->Rec->UUpmode){
                            case '0':// 余额
                                $result[$row['fid']][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case '1':// 支付宝
                                $result[$row['fid']][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case '2':// 授信支付
                                $result[$row['fid']][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case '3': // 自销
                                $result[$row['fid']][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case '4':// 现场支付
                                $result[$row['fid']][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            default:
                                break;
//                        }
                    }
                }
            }
        }
       // unset($fids);
       // unset($fid);
        if(count($result)){
        foreach($result as $key=>$val){
            foreach($val as $code => $v){
                if(!$code_area[$key][$code]){//没查询过这个地区//没查询过这个地区的代理商
                    $sel_province_city = "select province,city,parent_province from pft_area_code_concat 
                                                  where n_code=$code limit 1";
                    $GLOBALS['le']->query($sel_province_city);
                    $GLOBALS['le']->fetch_assoc();
                    $code_area[$key][$code]=array(
                         'c_code'=> $code,
                         'province'=> $GLOBALS['le']->f('province'),
                         'city'=> $GLOBALS['le']->f('city'),
                         'parent_province'=>$GLOBALS['le']->f('parent_province')
                    );
                    $sel_agency_p = "select inviterID from pft_distributor_agency where fid={$key} and
                                    darea_code={$code_area[$key][$code]['parent_province']} limit 1";
                    $GLOBALS['le']->query($sel_agency_p);
                    $GLOBALS['le']->fetch_assoc();
                    $agency_p = $GLOBALS['le']->f('inviterID');
                    $agency_p = $agency_p!='' ? $agency_p : $key;
                    $code_area[$key][$code]['agency_p'] = $agency_p;
                    $agency[$code_area[$key][$code]['agency_p']] = $agency_p;

                    if($code_area[$key][$code]['parent_province'] == $code){//直辖市
                        $code_area[$key][$code]['agency_c'] = $agency_p;
                    }else{
                        $sel_agency_c = "select inviterID from pft_distributor_agency where
                                      fid={$key} and darea_code=$code limit 1";
                        $GLOBALS['le']->query($sel_agency_c);
                        $GLOBALS['le']->fetch_assoc();
                        $agency_c = $GLOBALS['le']->f('inviterID');
                        $agency_c = $agency_c!='' ? $agency_c : $agency_p;
                        $code_area[$key][$code]['agency_c'] = $agency_c;
                        $agency[$code_area[$key][$code]['agency_c']] = $agency_c;
                    }
                }
                $result[$key][$code]['city'] = $code_area[$key][$code]['city'];
                $result[$key][$code]['title']= $code_area[$key][$code]['province'];
                $result[$key][$code]['agency_c'] = $code_area[$key][$code]['agency_c'];//市代理
                $result[$key][$code]['agency_p'] = $code_area[$key][$code]['agency_p'];
            }
            // $fid .=$key.',';
        }}
        // $fid = substr($fid,0,-1);
        // $sel = "SELECT dtitle,fid from pft_distributor_union_SE where fid in($fid) ";
        // $GLOBALS['le']->query($sel);
        // while($row= $GLOBALS['le']->fetch_assoc()){
            // $fids[$row['fid']] = $row['dtitle'];
        // }
//        exit;
        break;           
        
    case 'lid':        
        while($row= $GLOBALS['le']->fetch_assoc()){
            $orderid_1[$row['orderid']] = $row['orderid'];
            $x = simplexml_load_string($s->Order_Globle_Search('', '', '', '', '', '', '', '', '', '',
                                                    '', '', $row['orderid'], '', '', '', '', '', '1', '1',
                                                    '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
            $x = pmode($x);
            if((int)$x->Rec->UUpmode == 3)
                $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
            $arrId = (int)$x->Rec->UUlid;
            $result[$arrId][$arrId]['title']=(string )$x->Rec->UUltitle;
            ++$result[$arrId][$arrId]['order'];
            $result[$arrId][$arrId]['tnum'] += $x->Rec->UUtnum;
            $result[$arrId][$arrId]['money'] += (float)$x->Rec->UUtnum*(float)$x->Rec->UUtprice;
            switch((int )$x->Rec->UUpmode){
                case 0:// 余额
                    $result[$arrId][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 1:// 支付宝
                    $result[$arrId][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 2:// 授信支付
                    $result[$arrId][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 3: // 自销
                    $result[$arrId][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 4:// 现场支付
                    $result[$arrId][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                default:
                    break;
            }
        }
        $alli_num = $id_case['member'];
        if($_GET['alli_num'])
            $alli_num = in_array($_GET['alli_num'],$id_case_array['member']) ? $_GET['alli_num'] : '';
         if($alli_num){
            $sel_lirun = "select orderid,fid from pft_distributor_union_lirun where fid in({$alli_num})".$where_time;
            $GLOBALS['le']->query($sel_lirun);
            while($row = $GLOBALS['le']->fetch_assoc()){
                if(!in_array($row['orderid'],$orderid_1)){//这里查出来的订单 都是分销商的
                     $x = simplexml_load_string($s->Order_Globle_Search(
                            '', '', '', '', '', '', '', '', '', '',
                            '', '', $row['orderid'], '', '', '', '', '', '', '',
                            '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
//                    if((int)$x->Rec->UUmid == $_SESSION['sid']){//判断下这个订单的分销商是不是登录者的 不是就unset
                        $x = pmode($x);
                        if((int)$x->Rec->UUpmode == 3)
                            $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
                        $arrId = (int)$x->Rec->UUlid;
                        $result[$arrId][$arrId]['title']=(string )$x->Rec->UUltitle;
                        ++$result[$arrId][$arrId]['order'];
                        $result[$arrId][$arrId]['tnum'] += $x->Rec->UUtnum;
                        $result[$arrId][$arrId]['money'] += (float)$x->Rec->UUtnum*(float)$x->Rec->UUtprice;
                        switch((int )$x->Rec->UUpmode){
                            case 0:// 余额
                                $result[$arrId][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 1:// 支付宝
                                $result[$arrId][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 2:// 授信支付
                                $result[$arrId][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 3: // 自销
                                $result[$arrId][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 4:// 现场支付
                                $result[$arrId][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            default:
                                break;
//                        }
                    }
                }
            }
        }
        
        break;
        
    case 'tid':
        while($row= $GLOBALS['le']->fetch_assoc()){
            $orderid_1[$row['orderid']] = $row['orderid'];
            $x = simplexml_load_string($s->Order_Globle_Search(
                        '', '', '', '', '', '', '', '', '', '',
                        '', '', $row['orderid'], '', '', '', '', '', '', '',
                        '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
            $x = pmode($x);
            if((int)$x->Rec->UUpmode == 3)
                $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
            $arrId = (int)$x->Rec->UUtid;
            $result[$arrId][$arrId]['title'] = (string )$x->Rec->UUltitle.(string )$x->Rec->UUttitle;
            ++$result[$arrId][$arrId]['order'];
            $result[$arrId][$arrId]['tnum'] += $x->Rec->UUtnum;
            $result[$arrId][$arrId]['money'] += (int)$x->Rec->UUtnum*(int)$x->Rec->UUtprice;
            switch((int )$x->Rec->UUpmode){
                case 0:// 余额
                    $result[$arrId][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 1:// 支付宝
                    $result[$arrId][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 2:// 授信支付
                    $result[$arrId][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 3: // 自销
                    $result[$arrId][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 4:// 现场支付
                    $result[$arrId][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                default:
                    break;
            }
        }
        $alli_num = $id_case['member'];
        if($_GET['alli_num'])
            $alli_num = in_array($_GET['alli_num'],$id_case_array['member']) ? $_GET['alli_num'] : '';
         if($alli_num){
            $sel_lirun = "select orderid,fid from pft_distributor_union_lirun where fid in({$alli_num})".$where_time;
            $GLOBALS['le']->query($sel_lirun);
            while($row = $GLOBALS['le']->fetch_assoc()){
                if(!in_array($row['orderid'],$orderid_1)){//这里查出来的订单 都是分销商的 
                     $x = simplexml_load_string($s->Order_Globle_Search(
                                '', '', '', '', '', '', '', '', '', '',
                                '', '', $row['orderid'], '', '', '', '', '', '', '',
                                '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
//                    if((int)$x->Rec->UUmid == $_SESSION['sid']){//判断下这个订单的分销商是不是登录者的 不是就unset
                        $x = pmode($x);
                        if((int)$x->Rec->UUpmode == 3)
                            $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
                        $arrId = (int)$x->Rec->UUtid;
                        $result[$arrId][$arrId]['title'] = (string )$x->Rec->UUltitle.(string )$x->Rec->UUttitle;
                        ++$result[$arrId][$arrId]['order'];
                        $result[$arrId][$arrId]['tnum'] += $x->Rec->UUtnum;
                        $result[$arrId][$arrId]['money'] += (int)$x->Rec->UUtnum*(int)$x->Rec->UUtprice;
                        switch((int)$x->Rec->UUpmode){
                            case 0:// 余额
                                $result[$arrId][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 1:// 支付宝
                                $result[$arrId][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 2:// 授信支付
                                $result[$arrId][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 3: // 自销
                                $result[$arrId][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 4:// 现场支付
                                $result[$arrId][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            default:
                                break;
//                        }
                    }
                }
            }
        }
        break;
                
    case 'mid':
        while($row= $GLOBALS['le']->fetch_assoc()){
            $orderid_1[$row['orderid']] = $row['orderid'];
            $x = simplexml_load_string($s->Order_Globle_Search(
                    '', '', '', '', '', '', '', '', '', '',
                    '', '', $row['orderid'], '', '', '', '', '', '', '',
                    '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
            $x = pmode($x);
            if((int)$x->Rec->UUpmode == 3)
                $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
            $arrId = (int)$x->Rec->UUmid;
            $result[$arrId][$arrId]['title']=(string)$x->Rec->UUdname;
            ++$result[$arrId][$arrId]['order'];
            $result[$arrId][$arrId]['tnum'] +=$x->Rec->UUtnum;
            $result[$arrId][$arrId]['money'] += (int)$x->Rec->UUtnum*(int)$x->Rec->UUtprice;
            switch((int)$x->Rec->UUpmode){
                case 0:// 余额
                    $result[$arrId][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 1:// 支付宝
                    $result[$arrId][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 2:// 授信支付
                    $result[$arrId][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 3: // 自销
                    $result[$arrId][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                case 4:// 现场支付
                    $result[$arrId][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                    break;
                default:
                    break;
            }
        }
        $alli_num = $id_case['member'];
        if($_GET['alli_num'])
           $alli_num = in_array($_GET['alli_num'],$id_case_array['member']) ? $_GET['alli_num'] : '';
         if($alli_num){
            $sel_lirun = "select orderid,fid from pft_distributor_union_lirun where fid in({$alli_num})".$where_time;
            $GLOBALS['le']->query($sel_lirun);
            while($row = $GLOBALS['le']->fetch_assoc()){
                if(!in_array($row['orderid'],$orderid_1)){//这里查出来的订单 都是分销商的 
                     $x = simplexml_load_string($s->Order_Globle_Search('', '', '', '', '', '', '', '', '', '',
                                                                '', '', $row['orderid'], '', '', '', '', '', '1', '1',
                                                                '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
//                    if((int)$x->Rec->UUmid == $_SESSION['sid']){//判断下这个订单的分销商是不是登录者的 不是就unset
                        $x = pmode($x);
                        if((int)$x->Rec->UUpmode == 3)
                            $x->Rec->UUpmode = ProOrder::pmode_local((int)$x->Rec->UUtid)== 'local'? 4:3;
                        $arrId = (int)$x->Rec->UUmid;
                        $result[$arrId][$arrId]['title']=(string)$x->Rec->UUdname;
                        ++$result[$arrId][$arrId]['order'];
                        $result[$arrId][$arrId]['tnum'] +=$x->Rec->UUtnum;
                        $result[$arrId][$arrId]['money'] += (int)$x->Rec->UUtnum*(int)$x->Rec->UUtprice;
                        switch((int)$x->Rec->UUpmode){
                            case 0:// 余额
                                $result[$arrId][$arrId]['pmode0'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 1:// 支付宝
                                $result[$arrId][$arrId]['pmode1'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 2:// 授信支付
                                $result[$arrId][$arrId]['pmode2'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 3: // 自销
                                $result[$arrId][$arrId]['pmode3'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            case 4:// 现场支付
                                $result[$arrId][$arrId]['pmode4'] += (int)$x->Rec->UUtnum * (int)$x->Rec->UUtprice;
                                break;
                            default:
                                break;
//                        }
                    }
                }
            }
        }
        break;
}


if($_GET['cs']){
    echo "<pre>";
//    print_r($agency);
 print_r($result);
    echo "</pre>";
// exit;
}
if(count($agency)){
    foreach($agency as $v){
        $agency_num .= $v.',';
    }
    $agency_num = substr($agency_num,0,-1);
    $sel_agency_name = "select id,dname from pft_member where id in ($agency_num)";
    $le->query($sel_agency_name);
    while($row = $le->fetch_assoc()){
        $agency[$row['id']] = $row['dname'];
    }
}
/*最终得到的$r是两层循环的数组
1 盟主的编号为键名
2 根据$by来区别统计的主要层
*/
$from='alliance';
include("tpl/allianceOrderReport.tpl");
include("common/footer.html");

function pmode($order){
    if(
        (string)$order->Rec->UUaids!='0' &&  //供销参与人数超过2个
        $_SESSION['sid']!=(int)$order->Rec->UUmid && //不是最后一级
        $_SESSION['sid']!=1  //不是管理员
    )
    {
        $aid_money = unserialize($order->Rec->UUaids_money);
        if($GLOBALS['aom']=='mid'){//买到的不需要做 先不管了
            foreach($aid_money as $v){
                if($v[0]==$_SESSION['sid']){
                    $order->Rec->UUpmode = $v[1]== 1 ?
                        0//余额
                        :2//授信
                    ;
                    break;
                }
            }
        }
        else{
            //查找自己在aids中的位置 然后在$aid_money中找自己的下一级在哪里
            $aid_position = (string)$order->Rec->UUaids;
            $aid_position = explode(',',$aid_position);
            $position = array_search($_SESSION['sid'],$aid_position);
            foreach($aid_money as $v){
                if($v[0]==$aid_position[$position+1]){
                    $order->Rec->UUpmode = $v[1]== 1 ? 0 :2;
                    break;
                }
            }
            //计算转分销价格
            $UUaids_price = (string)$order->Rec->UUaids_price;
            $UUaids_price = explode(',',$UUaids_price);
            $order->Rec->UUtprice = $UUaids_price[$position];
        }
    }
    return $order;
}
/*二手转分销处理
function secondProOrderHandle($row){
		if($row->UUsellid==0){
			$row->UUsellid = $row->UUaid;
		}
		if($row->UUbuyid==0){
			$row->UUbuyid = $row->UUmid;
		}
        if ((string )$row->UUaids&&(string )$row->UUaids_price) { //是多级供应产品
			$UUtprices = array();
			$UUaids = explode(",", (string )$row->UUaids);
			$UUaids_price = explode(",", (string )$row->UUaids_price);
			//转供应商家查已卖出产品
			if ($this->params['aid']) {
				
				//转供应价格 如UUaids:81,94 UUaids_price:4. 就是81供应价是4，94分销价是4
				for ($i = 0; $i < count($UUaids) - 1; $i++) {
					$UUtprices[$UUaids[$i]] = $UUaids_price[$i];
				}
				$UUtprices[(string)$row->UUaid]=(string)$row->UUtprice;
				$aids = explode("|", $this->params['aid']);
				foreach($UUtprices as $aid=>$tprice){
					if(in_array($aid,$aids)){
						$row->SecondHand = 1;
						$pos = array_search($aid,$UUaids);
						$row->UUmid = $UUaids[$pos + 1];
						$row->UUtprice = $tprice;
						$row->UUtotalmoney = $row->UUtprice * $row->UUtnum;
					}
				}
			}
			if ($this->params['mid']) {
				$UUmids=$UUaids;
				$UUmids[]=(string)$row->UUmid;
				$UUtprice[(string)$row->UUmid]=(string)$row->UUtprice;
				for ($i = 1; $i < count($UUaids); $i++) {
					$UUtprices[$UUaids[$i]] = $UUaids_price[$i - 1];
				}
				$mids = explode("|", $this->params['mid']);
				//从最初分销商逐级查询，优先显示多级价
				foreach($UUtprices as $mid=>$tprice){
					if(in_array($mid,$mids)){
						$row->SecondHand = 1;
						$pos = array_search($mid,$UUmids);
						$row->UUaid = $UUmids[$pos - 1];
						$row->UUtprice =$tprice;
						$row->UUtotalmoney = $row->UUtprice * $row->UUtnum;
					}
				}				
			}
		}
        return $row;
    }
 */
